package com.wswy.wzcx.ui.activity.news

import android.graphics.*
import android.support.v4.content.ContextCompat
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.R
import com.wswy.wzcx.widget.drawable.BaseDrawable

class TopResultBgDrawable: BaseDrawable() {

    private val topPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    private val maskPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.WHITE
    }

    private val rectF = RectF()

    override fun onBoundsChange(bounds: Rect?) {
        super.onBoundsChange(bounds)
        rectF.set(rect)

        val gradient = LinearGradient(0f, 0f, rectF.width(), rectF.height(),
                intArrayOf(ContextCompat.getColor(AppContext.getContext(), R.color.startColor_1),  ContextCompat.getColor(AppContext.getContext(), R.color.endColor_1)), null, Shader.TileMode.CLAMP)

        topPaint.shader = gradient
    }


    override fun draw(canvas: Canvas?) {

        canvas?.run {
            drawRect(rectF,topPaint)

            drawRect(rectF,maskPaint)
        }
    }


    fun colorMask(ratio:Float){

        topPaint.alpha = ((1-ratio) * 255f).toInt()

        maskPaint.color =  (ratio * 255f).toInt()

        invalidateSelf()
    }

}